//
//  BaseViewController.swift
//  ProjectApp
//
//  Created by 邹琳 on 2019/5/31.
//  Copyright © 2019 邹琳. All rights reserved.
//

import UIKit
import SnapKit

class BaseViewController: UIViewController {
	/** 自定义navigation bar */
    let naviView: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.white
        return temp
    }()
    /** navigationBar 的分割线 */
    let naviLineView: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor(0xE9E9E9)
        return temp
    }()
    /** 标题 */
    let labelTitle: UILabel = {
        let label = UILabel()
        label.text = "Navi"
        label.font = UIFont.mediumFont(18)
        label.textColor = UIColor(0x131313)
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    let leftItem: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.titleLabel?.textColor = UIColor(0x131313)
        button.setImage(UIImage(named: "return")!, for: .normal)
        return button
    }()
    
    let rightItem: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.titleLabel?.textColor = UIColor(0x131313)
        return button
    }()
    
    @objc func leftAction() {
        print("左边按钮")
        if self.presentedViewController != nil {
            self.dismiss(animated: true, completion: nil)
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func rightAction() {
        print("点击右键")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.navigationBar.isHidden = true
        self.view.backgroundColor = UIColor.white
        self.tabBarController?.tabBar.isTranslucent = false
        self.navigationController?.navigationBar.isTranslucent = false
        /* 默认隐藏分割线 */
        self.naviLineView.isHidden = true
		
        self.view.addSubview(self.naviView)
        self.naviView.addSubview(self.naviLineView)
        self.naviView.addSubview(self.labelTitle)
        self.naviView.addSubview(self.leftItem)
        self.naviView.addSubview(self.rightItem)
        
        self.leftItem.addTarget(self, action: #selector(leftAction), for: .touchUpInside)
        self.rightItem.addTarget(self, action: #selector(rightAction), for: .touchUpInside)
        
        var height = 64
        if UIDevice.current.modelName.contains("iPhoneX") || UIDevice.current.modelName.contains("Simulator") {
            height = 88
        }
        
        self.naviView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(self.view)
            make.height.equalTo(height)
        }
        
        self.naviLineView.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.left.right.bottom.equalTo(self.naviView)
        }
        
        self.labelTitle.snp.makeConstraints { (make) in
            make.left.equalTo(100)
            make.right.equalTo(-100)
            make.bottom.equalTo(-12)
        }
        
        self.leftItem.snp.makeConstraints { (make) in
            make.left.equalTo(5)
            make.height.width.equalTo(40)
            make.centerY.equalTo(self.labelTitle)
        }
        
        self.rightItem.snp.makeConstraints { (make) in
            make.right.equalTo(-5)
            make.height.width.equalTo(40)
            make.centerY.equalTo(self.labelTitle)
        }
    }

}
